package entry;

import constant.Constant;
import utils.ItextUtil;
import utils.ReadAndWriteFile;

import java.util.List;

public class Rule {
    //竖,自翻,4+,2x2,1* 4* 2 3
    private boolean is_heng;
    private boolean is_zhengfan;
    private String p;
    private int col;
    private int row;
    private String rule;
    private int lian;

    public Rule(String str) {
        String[] arr = str.split(",");
        if (arr[0].equals("单联")) {
            this.lian = 1;
        } else if (arr[0].equals("双联")) {
            this.lian = 2;
        } else if (arr[0].equals("三联")) {
            this.lian = 3;
        }
        boolean is_heng = arr[1].equals("横");
        boolean is_zhengfan = arr[2].equals("正反");
        String p = arr[3];
        int col = Integer.valueOf(arr[4].split("x")[0]);
        int row = Integer.valueOf(arr[4].split("x")[1]);
        String rule = arr[5];
        this.is_heng = is_heng;
        this.is_zhengfan = is_zhengfan;
        this.p = p;
        this.col = col;
        this.row = row;
        this.rule = rule;
    }

    public Rule(boolean is_heng, boolean is_zhengfan, int col, int row, int lian) {
        //new Rule(is_heng,ysfs.equals("自翻"),col,row,lian);
        this.is_heng = is_heng;
        this.is_zhengfan = is_zhengfan;
//        this.p = p;
        this.col = col;
        this.row = row;
//        this.rule = rule;
        this.lian = lian;
    }

    public static void main(String[] args) {
        String rule = getRule(false, true, 2, 2, 1, 8);
        System.out.println(rule);
    }
    public static String getRule(boolean is_heng, boolean is_zhengfan, int col, int row, int lian, int p) {
//        单联,竖,自翻,4+,2x2,1* 4* 2 3
        String heng = "竖";
        if (is_heng) {
            heng = "横";
        }
        String njia = "";
        if (p >= col * row) {
            njia = col * row + "+";
        } else {
            njia = p + "";
        }
        String yinshuafangshi = "自翻";
        if (is_zhengfan) {
            yinshuafangshi = "正反";

            if (p >= col * row*2/lian) {
                njia = col * row*2/lian + "+";
            } else {
                njia = p + "";
            }
        }
        String axa = col + "x" + row;
        String lian_str = "";
        if (lian == 1) {
            lian_str = "单联";
        } else if (lian == 2) {
            lian_str = "双联";
        } else if (lian == 3) {
            lian_str = "三联";
        } else {
            return "";
        }
        String f = lian_str+","+heng+","+yinshuafangshi+","+njia+","+axa;
        System.out.println(f);
        List<String> list_rule = ReadAndWriteFile.readtFile(Constant.file_rule, "utf-8");
        for(String str:list_rule){
            if(str.indexOf(f)!=-1){
                System.out.println(f+str);
                String rule =  str.split(",")[str.split(",").length-1];
                if(Constant.alert_info){
                    ItextUtil.alertInfo(str,"规则");
                }
                return str.split(",")[str.split(",").length-1];
            }
        }

        ItextUtil.alert("规则没有找到.\n"+f,"规则");
        return "";
    }

    public Rule() {
    }

    public int getLian() {
        return lian;
    }

    public void setLian(int lian) {
        this.lian = lian;
    }

    public boolean isIs_heng() {
        return is_heng;
    }

    public void setIs_heng(boolean is_heng) {
        this.is_heng = is_heng;
    }

    public boolean isIs_zhengfan() {
        return is_zhengfan;
    }

    public void setIs_zhengfan(boolean is_zhengfan) {
        this.is_zhengfan = is_zhengfan;
    }

    public String getP() {
        return p;
    }

    public void setP(String p) {
        this.p = p;
    }

    public int getCol() {
        return col;
    }

    public void setCol(int col) {
        this.col = col;
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public String getRule() {
        return rule;
    }

    public void setRule(String rule) {
        this.rule = rule;
    }
}
